package com.xiaoyu.lanling.feature.board.viewholder

import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.emoji.widget.EmojiTextView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.base.view.OnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.board.model.BoardItem
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.view.UserAvatarDraweeView

class BoardViewHolder : ViewHolderBase<BoardItem>() {
    private lateinit var mRanking: TextView
    private lateinit var mAvatar: UserAvatarDraweeView
    private lateinit var mName: EmojiTextView
    private lateinit var mIndex: TextView

    private val onAvatarClickListener = object : OnClickDebounceListener() {

        override fun onClickDebounce(v: View) {
            val activity = App.getInstance().topActivity ?: return
            val itemData = v.getViewTag<BoardItem>() ?: return
            Router.instance.gotoUserActivity(activity,itemData.user)
        }
    }

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View {
        val convertView = layoutInflater.inflate(R.layout.item_board, parent, false)
        mRanking = convertView.findViewById(R.id.rank)
        mAvatar = convertView.findViewById(R.id.avatar)
        mName = convertView.findViewById(R.id.name)
        mIndex = convertView.findViewById(R.id.index)
        mAvatar.setOnClickListener(onAvatarClickListener)
        return convertView
    }

    override fun showData(position: Int, itemData: BoardItem) {
        mRanking.text = itemData.rank
        ImageViewUtil.loadImage(mAvatar, itemData.loadParam)
        mAvatar.setViewTag(itemData)
        mName.text = itemData.user.name
        mIndex.text = itemData.index
    }
}